package com.spider.admin.agent.conf;

import com.spider.admin.conf.ENVConf;
import com.spider.admin.utils.CommonUtil;
import com.spider.admin.utils.FileUtils;
import com.spider.admin.utils.OSCommandUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class HttpConf {
    @Autowired
    private ENVConf envConf;

    private String getWebConfInfo(){
        return "#user  spider;\n" +
                "worker_processes  1;\n" +
                "\n" +
                "#error_log  logs/error.log;\n" +
                "#error_log  logs/error.log  notice;\n" +
                "#error_log  logs/error.log  info;\n" +
                "\n" +
                "\n" +
                "#pid        logs/nginx.pid;\n" +
                "\n" +
                "events {\n" +
                "    worker_connections  1024;\n" +
                "}\n" +
                "http {\n" +
                "    include       mime.types;\n" +
                "    charset utf-8;\n" +
                "    charset_types text/css text/plain application/json application/javascript;\n" +
                "    resolver 8.8.8.8;\n" +
                "    log_format  main  '$remote_addr - \"$request\" ';\n" +
                "\n" +
                "    #access_log  logs/access.log  main;\n" +
                "    sendfile        on;\n" +
                "    #tcp_nopush     on;\n" +
                "\n" +
                "    #keepalive_timeout  0;\n" +
                "    keepalive_timeout  65;\n" +
                "    \n" +
                "    lua_shared_dict logs_data 1m;\n" +
                "    init_worker_by_lua_file hgg-lua/timer.lua;\n" +
                "    #gzip  on;\n" +
                "\n" +
                "\n" +
                "\tupstream managerApiServer {\n" +
                "\t\tserver 10.1.245.177:8888;\n" +
                "\t}\n" +
                "\tserver {\n" +
                "\t\tlisten       80;\n" +
                "\t\tserver_name  admin.duli123.com;\n" +
                "\n" +
                "\t\tlocation / {\n" +
                "\t\t\troot html;\n" +
                "\t\t\tindex index.html index.htm;\n" +
                "\t\t}\n" +
                "\n" +
                "\t\tlocation /api/ {\n" +
                "\t\t\taccess_by_lua_file hgg-lua/access_control.lua;\n" +
                "\t\t\tproxy_pass   http://managerApiServer/;\n" +
                "\t\t}\n" +
                "\t\tlocation /verify { #验证token服务\n" +
                "\t\t\tinternal;           #内部location，不能外部调用\n" +
                "\t\t\tproxy_pass   http://managerApiServer/verify;\n" +
                "\t\t\tproxy_set_header Content-type \"application/json\";\n" +
                "\t\t}\n" +
                "\t}\n" +
                "\tserver {\n" +
                "\t\tlisten       80;\n" +
                "\t\tserver_name  admin-dev.duli123.com;\n" +
                "\t\tlocation / {\n" +
                "\t\t\tproxy_pass   http://127.0.0.1:9900;\n" +
                "\t\t}\n" +
                "\t\tlocation /api/ {  \n" +
                "\t\t\taccess_by_lua_file hgg-lua/access_control.lua;          \n" +
                "\t\t\tproxy_pass   http://managerApiServer/;\n" +
                "\t\t}\n" +
                "\t\tlocation /verify { #验证token服务\n" +
                "\t\t\tinternal;           #内部location，不能外部调用\n" +
                "\t\t\tproxy_pass   http://managerApiServer/verify;\n" +
                "\t\t\tproxy_set_header Content-type \"application/json\";\n" +
                "\t\t}\n" +
                "\t}\n" +
                "}";
    }
    private String getHttpConfInfo(){
        return "http {\n" +
                "    include       mime.types;\n" +
                "    charset utf-8;\n" +
                "    charset_types text/css text/plain application/json application/javascript;\n" +
                "    resolver 8.8.8.8;\n" +
                "    log_format  main  '$remote_addr - \"$request\" ';\n" +
                "\n" +
                "    #access_log  logs/access.log  main;\n" +
                "    sendfile        on;\n" +
                "    #tcp_nopush     on;\n" +
                "\n" +
                "    #keepalive_timeout  0;\n" +
                "    keepalive_timeout  65;\n" +
                "    \n" +
                "    lua_shared_dict logs_data 1m;\n" +
                "    init_worker_by_lua_file hgg-lua/timer.lua;\n" +
                "    #gzip  on;\n" +
                "\n" +
                "    #管理端服务配置\n" +
                "    include ../spider_conf/server-admin/v1.0/*.conf;\n" +
                "\n" +
                "    #代理服务配置\n" +
                "    include ../spider_conf/server-example/v1.0/*.conf; \n" +
                "    include ../spider_conf/server-example-ui/v1.0/*.conf;\n" +
                "}";
    }

    public void writeHttpConf() {
        try {
            String commonConfPath = null;
            if (OSCommandUtil.isWinOS()) {
                commonConfPath = envConf.getUiContainerBasePath() + "/spider_conf/http.conf";
            } else {
                commonConfPath = envConf.getUiContainerBasePath() + "/nginx/spider_conf/http.conf";
            }
            FileUtils.writeFile(commonConfPath, getHttpConfInfo());
            log.info("生成HTTP配置");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("HTTP配置生成异常：" + e.getMessage());
        }
    }
}
